/**
 * This class creates the three police station servant objects
 * and handle teh CORBA references of these objects
 */
package com.comp6231.replica2.server;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

import org.omg.CORBA.ORB;
import org.omg.CORBA.ORBPackage.InvalidName;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.omg.PortableServer.POAPackage.ObjectAlreadyActive;
import org.omg.PortableServer.POAPackage.ObjectNotActive;
import org.omg.PortableServer.POAPackage.ServantAlreadyActive;
import org.omg.PortableServer.POAPackage.WrongPolicy;

import com.comp6231.replica2.server.helper.StationID;

/**
 * @author Farzana Alam
 * 
 */
public class StationServer implements Runnable {

	/**
	 * @param args
	 * @throws InvalidName
	 * @throws ServantAlreadyActive
	 * @throws WrongPolicy
	 * @throws ObjectNotActive
	 * @throws FileNotFoundException
	 * @throws AdapterInactive
	 * @throws ObjectAlreadyActive
	 */
	// public static void startServers(String[] args) throws InvalidName,
	// ServantAlreadyActive, WrongPolicy, ObjectNotActive,
	// FileNotFoundException, AdapterInactive, ObjectAlreadyActive {

	@Override
	public void run() {

		// Initialize the ORB
		ORB orb = ORB.init(new String[] { "" }, null);
		POA rootPOA;
		try {
			rootPOA = POAHelper.narrow(orb
					.resolve_initial_references("RootPOA"));
			// three different IDs for the servant classes
			byte[] SPVM_ID = "SPVM".getBytes();
			byte[] SPL_ID = "SPL".getBytes();
			byte[] SPB_ID = "SPB".getBytes();

			// Obtain a reference
			StationImpl montreal = new StationImpl(StationID.SPVM, 4567);
			StationImpl longuiel = new StationImpl(StationID.SPL, 5678);
			StationImpl brossard = new StationImpl(StationID.SPB, 6789);

			rootPOA.activate_object_with_id(SPVM_ID, montreal);
			rootPOA.activate_object_with_id(SPL_ID, longuiel);
			rootPOA.activate_object_with_id(SPB_ID, brossard);

			org.omg.CORBA.Object refSPVM = rootPOA.id_to_reference(SPVM_ID);
			org.omg.CORBA.Object refSPL = rootPOA.id_to_reference(SPL_ID);
			org.omg.CORBA.Object refSPB = rootPOA.id_to_reference(SPB_ID);

			// Translate to IOR and write to a file
			String ior_spvm = orb.object_to_string(refSPVM);
			// System.out.println(ior_spvm);

			String ior_spl = orb.object_to_string(refSPL);
			// System.out.println(ior_spl);

			String ior_spb = orb.object_to_string(refSPB);
			// System.out.println(ior_spb);

			PrintWriter file = new PrintWriter(System.getProperty("user.dir")
					+ "/src/com/comp6231/replica2/server/helper/ior_spvm.txt");
			file.println(ior_spvm);
			file.close();

			file = new PrintWriter(System.getProperty("user.dir")
					+ "/src/com/comp6231/replica2/server/helper/ior_spl.txt");
			file.println(ior_spl);
			file.close();

			file = new PrintWriter(System.getProperty("user.dir")
					+ "/src/com/comp6231/replica2/server/helper/ior_spb.txt");
			file.println(ior_spb);
			file.close();

			// Activate the ORB
			rootPOA.the_POAManager().activate();

			//System.out.println("Local Servers are started!");
			orb.run();
		} catch (InvalidName | ServantAlreadyActive | WrongPolicy
				| ObjectNotActive | FileNotFoundException | AdapterInactive
				| ObjectAlreadyActive e) {
			e.printStackTrace();
		}

	}

}
